package com.sensePlusVR.manager;

import com.alibaba.fastjson.JSON;
import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.bean.BannersDto;
import com.sensePlusVR.net.NetCallBack;
import com.sensePlusVR.net.RequestUtils;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.ConstantURL;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.MySharedPreferences;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

@SuppressWarnings("deprecation")
public class BannerManager extends BaseManager{

	private static final String TAG = BannerManager.class.getSimpleName();
	private static BannerManager INSTANTS;
	public static final int EventBannerList = 0;
	
	static public BannerManager getInstants(){
		if (INSTANTS == null){
			INSTANTS = new BannerManager();
		}
		return INSTANTS;
	}
	
	public List<BannersDto> getBannerList(int commonType,boolean update){
		List<BannersDto> bannersDtos = BannersDto.getBannerList();
		if(bannersDtos == null || bannersDtos.size() == 0 || update){
			DownloadTask task = new DownloadTask(commonType);
			task.start();
			return null;
		}
			return bannersDtos;
	}
	
	
	public class DownloadTask {
		private int commonType;
		
		public DownloadTask(){}
		
		public DownloadTask(int commonType){
			this.commonType = commonType;
		}
		
		
		private void updateBannerListDB(List<BannersDto> bannersDtos){
			
			MyLog.d(TAG, "getBannerList size 111 : " + (BannersDto.getBannerList() != null?BannersDto.getBannerList().size():0));
			
			BannersDto.delAllData();
			
			MyLog.d(TAG, "getBannerList size 222 : " + (BannersDto.getBannerList() != null?BannersDto.getBannerList().size():0));
			
			BannersDto.saveOrUpdate(bannersDtos);
			
			MyLog.d(TAG, "getBannerList size 333 : " + (BannersDto.getBannerList() != null?BannersDto.getBannerList().size():0));
			
		}
		
		public void start(){
			String url = ConstantURL.BANNER_LIST + commonType;
			String token = MySharedPreferences.getString2(VRApplication.getAppContext(),MySharedPreferences.NAME_NOT_CLEAR, Constant.LOGIN_SUCCESS_TOKEN);
			RequestUtils.clientToken.addHeader(Constant.STR_X_AUTH_TOKEN,token);
			RequestUtils.clientTokenGet(VRApplication.getAppContext(), url, new NetCallBack(){
				
				@Override
				public void onMySuccess(int statusCode, Header[] headers,
						JSONArray response) {
					super.onMySuccess(statusCode, headers, response);
					MyLog.d(TAG, "getBannerList onMySuccess statusCode : " + statusCode);
	    			if(response == null){
	    				MyLog.e(TAG, "getBannerList onMySuccess response == null");
	    				return;
	    			}
	    			MyLog.d(TAG, "getBannerList onMySuccess response : " + response);
	    			if(statusCode == Constant.HTTP_200){
	    				MyLog.d(TAG, "getBannerList successfully");
	    				List<BannersDto> bannersDtos = JSON.parseArray(response.toString(), BannersDto.class);
	    				updateBannerListDB(bannersDtos);
	    				callEventListener(EventBannerList, bannersDtos);
	    				
	    				
	    			}
				}
				
				@Override
				public void onMyFailure(int statusCode, Header[] headers,
						Throwable throwable, JSONObject errorResponse) {
					super.onMyFailure(statusCode, headers, throwable, errorResponse);
					MyLog.e(TAG, "getBannerList onMyFailure statusCode : " + statusCode);
	    	        if(statusCode == 0 || errorResponse == null){
	    	        	MyLog.e(TAG, "getBannerList errorJsonobject == null");
	    	        	return ;
	    	        }
	    	        MyLog.e(TAG, "getFeedbackList onMyFailure errorResponse : " + errorResponse.toString());
				}
				
			});
			
		}
		
	}
	
}
